package com.victor.dao;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.victor.entity.ContrastRecord;
import com.victor.entity.Publicity;
import com.victor.entity.User;
import com.victor.entity.Vote;

@Mapper
public interface VoteDao {

	// 通过openid查询该用户是否已经参加过投票
	@Select({ "SELECT * FROM user WHERE openid=#{openid}" })
	User ifJoinVote(@Param("openid") String openid);

	// 将user信息插入到数据库
	@Insert({ "INSERT INTO user (openid,identity,school,agegroup,voteaddress) VALUES (#{openid},#{identity},#{school},#{agegroup},#{voteaddress}) " })
	@Options(useGeneratedKeys = true, keyProperty = "id")
	int addUserMsg(User user);

	// 将votes信息插入到数据库
	@Insert({ "INSERT INTO vote (userid,picname,author,agegroup) VALUES (#{userid},#{picname},#{author},#{agegroup}) " })
	int addVote(Vote vote);

	// 通过书名和年龄段查询公示表是否存在该书
	@Select({ "SELECT * FROM publicity WHERE picname=#{picname} AND agegroup=#{agegroup}" })
	Publicity ifJoinPublicity(@Param("picname") String picname, @Param("agegroup") String agegroup);

	// 拿到投票次数,将次数加1,更新数据库
	@Update({ "UPDATE publicity SET numberofvote=#{numberofvote} WHERE id=#{id}" })
	int updPublicity(Publicity publicity);

	// 将书名,作者,投票次数1次,和年龄段存入数据库
	@Insert({ "INSERT INTO publicity (picname,author,numberofvote,agegroup) VALUES (#{picname},#{author},#{numberofvote},#{agegroup}) " })
	int insPublicity(Publicity publicity);

	// 获取投票公示信息
	@Select({ "SELECT * FROM publicity ORDER BY numberofvote DESC" })
	List<Publicity> getAllPublicity();

	// 查询参加投票人数
	@Select({ "SELECT count(*)  FROM user" })
	Integer getTotleUser();

	// 修改榜单信息
	int updPublicityContent(Publicity publicity);

	// 获取所有用户
	@Select({ "SELECT * FROM user" })
	List<User> getAllUser();

	// 通过openid查询user表
	@Select({ "SELECT * FROM user where openid=#{openid}" })
	User getUserByOpenid(@Param("openid") String openid);

	// 查询传入学校名的人投票了多少
	@Select({ "SELECT COUNT(*) FROM user WHERE school=#{school}" })
	Integer getSchoolCount(@Param("school") String school);

	// 查询传入的城市的人投票了多少
	@Select({ "SELECT COUNT(*) FROM user WHERE voteaddress=#{city}" })
	Integer getCityCount(@Param("city") String city);

	// 插如对比记录
	@Insert({ "INSERT INTO contrastrecord (picname,city1,city2,school1,school2,count1,count2) VALUES (#{picname},#{city1},#{city2},#{school1},#{school2},#{count1},#{count2})" })
	int addContrastRecord(ContrastRecord contrastRecord);

	// 查询历史对比列表
	@Select({ "SELECT * FROM contrastrecord ORDER BY id DESC" })
	List<ContrastRecord> getContrastRecordList();

	// 通过历史对比列表id查询详情
	@Select({ "SELECT * FROM contrastrecord WHERE id=#{crid}" })
	ContrastRecord getContrastRecordById(@Param("crid") String crid);

}
